package com.decryption;

import java.io.File;
import java.util.Scanner;

/**
 * @Author: shiziyi
 * @Date: 2020/3/17 13:51
 * @Description: TODO
 */
public class Secret {
    public static void main(String[] args) throws Exception {
        Scanner input = new Scanner(System.in);
        System.out.println("输入待操作的文件名");
        String fileName = input.nextLine();
        String[] strArray = fileName.split("\\.");
        String newName = strArray[0] + "after." + strArray[1];
        System.out.println("输入密文参数");
        String key = input.nextLine();
        System.out.println("输入数字来执行功能");
        System.out.println("1为加密，2为解密");
        int i = input.nextInt();
        File file = new File(fileName);
        if (file.exists()){
            System.out.println("元文件解析成功");
            switch (i){
                case 1:
                    encryption(fileName,newName,key);
                    break;
                case 2:
                    decryption(fileName,newName,key);
                    break;
                default:
                    System.out.println("输入错误");
                    break;
            }
        }
    }

    /**
     * 加密
     * @throws Exception
     * @param fileName
     * @param newName
     * @param key
     */
    private static void encryption(String fileName, String newName, String key) throws Exception {
        Encryption encryption = new Encryption(key);
        encryption.encrypt(fileName, newName);
        System.out.println("加密成功，新文件："+newName);
    }

    /**
     * 解密
     * @throws Exception
     * @param fileName
     * @param newName
     * @param key
     */
    private static void decryption(String fileName, String newName, String key) throws Exception {
        Encryption encryption = new Encryption(key);
        encryption.decrypt(fileName, newName);
        System.out.println("解密成功，新文件："+newName);
    }


}
